/**
 * El código ha sido adaptado para que haga una única lectura y finalice el proceso.
 */
package JMSInternationalEventServer;

import java.sql.SQLException;

import JMSInternationalEventServer.dao.InternationalEventServerDAO;

import javax.jms.*;
import javax.naming.*;

import corbaServer.EventDO;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

public class JMSInternationalEventServer
{
	public static void main(String[] args)
	{
		InternationalEventServerDAO dao = new InternationalEventServerDAO();

		// debug
		try
		{
			dao.connect();
		}
		catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}

		List<EventDO> events = dao.getEvents();
		
		System.out.println(events);

		// /////

		String queueName = null;
		Context jndiContext = null;
		QueueConnectionFactory queueConnectionFactory = null;
		QueueConnection queueConnection = null;
		QueueSession queueSession = null;
		Queue queue = null;
		QueueSender queueSender = null;
		ObjectMessage objectMessage = null;
		queueName = "InternationalEventsJMSQueue"; // lo pongo a mano
//		System.out.println("Queue name is " + queueName);
		try
		{
			Properties props = new Properties();
			props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
					"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
			props.setProperty(Context.PROVIDER_URL, "tcp://localhost:8161");
			props.load(new FileInputStream("EventReservationCore/src/JMSInternationalEventServer/jndi.properties"));
			jndiContext = new InitialContext(props);
		}
		catch (NamingException e)
		{
			System.out.println("Could not create JNDI API context: "
					+ e.toString());
			System.exit(1);
		}

		catch (IOException ioe)
		{
			System.out.println("Could not find properties file: "
					+ ioe.toString());
			System.exit(1);
		}
		try
		{
			queueConnectionFactory = (QueueConnectionFactory) jndiContext
					.lookup("QueueConnectionFactory");
			queue = (Queue) jndiContext.lookup(queueName);
		}
		catch (NamingException e)
		{
			System.out.println("JNDI API lookup failed: " + e.toString());
			System.exit(1);
		}
		try
		{
			queueConnection = queueConnectionFactory.createQueueConnection();
			queueSession = queueConnection.createQueueSession(false,
					Session.AUTO_ACKNOWLEDGE);
			queueSender = queueSession.createSender(queue);
			objectMessage = queueSession.createObjectMessage();
			for (EventDO eventDO : events)
			{
				EventJMSDTO eventJMSDTO = new EventJMSDTO(eventDO.getName(), eventDO.getDate(),eventDO.getDescription(), eventDO.getPlaces());
				objectMessage.setObject(eventJMSDTO);
				System.out.println("Sending object message: "
						+ eventDO.getName() + " " + eventDO.getDate() + " " + eventDO.getDescription() + " " + eventDO.getPlaces());
				queueSender.send(objectMessage);
			}
			queueSender.send(queueSession.createMessage());
		}
		catch (JMSException e)
		{
			System.out.println("Exception occurred: " + e.toString());
		}
		finally
		{
			if (queueConnection != null)
			{
				try
				{
					queueConnection.close();
				}
				catch (JMSException e)
				{
				}
			}
		}
	}
}

/* Este sería el código para hacerlo asíncrono mediante un listener
 
package JMSInternationalEventServer;

import java.sql.SQLException;

import JMSInternationalEventServer.dao.InternationalEventServerDAO;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.jms.*;
import javax.naming.*;

import corbaServer.EventDO;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

public class JMSInternationalEventServer
{
	public static void main(String[] args)
	{
		InternationalEventServerDAO dao = new InternationalEventServerDAO();

		// debug
		try
		{
			dao.connect();
		}
		catch (ClassNotFoundException e)
		{
			// todo Auto-generated catch block
			e.printStackTrace();
		}
		catch (SQLException e)
		{
			// todo Auto-generated catch block
			e.printStackTrace();
		}

		List<EventDO> events = dao.getEvents();

		System.out.println(events);

		// /////

		String topicName = null;
		Context jndiContext = null;
		TopicConnectionFactory topicConnectionFactory = null;
		TopicConnection topicConnection = null;
		TopicSession topicSession = null;
		Topic topic = null;
		TopicPublisher topicPublisher = null;
		TextMessage message = null;
		final int NUM_MSGS;
//		if ((args.length < 1) || (args.length > 2))
//		{
//			System.out
//					.println("Usage: java SimpleTopicPublisher <topic-name> [<number-ofmessages>]");
//			System.exit(1);
//		}
		topicName = "MyIso2Topic"; //todo
		System.out.println("Topic name is " + topicName);
		if (args.length == 2)
		{
			NUM_MSGS = (new Integer(args[1])).intValue();
		}
		else
		{
			NUM_MSGS = 1;
		}
		try
		{
			Properties props = new Properties();
			props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
					"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
			props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");
			props.load(new FileInputStream(
					"/home/ander/workspace/ISOProject/EventReservationCore/src/JMSInternationalEventServer/jndi.properties")); // TODO
			jndiContext = new InitialContext(props);
		}
		catch (NamingException e)
		{
			System.out.println("Could not create JNDI API context: "
					+ e.toString());
			e.printStackTrace();
			System.exit(1);
		}
		catch (IOException ioe)
		{
			System.out.println("Could not find properties file: "
					+ ioe.toString());
			System.exit(1);
		}
		try
		{
			topicConnectionFactory = (TopicConnectionFactory) jndiContext
					.lookup("TopicConnectionFactory");
			topic = (Topic) jndiContext.lookup(topicName);
		}
		catch (NamingException e)
		{
			System.out.println("JNDI API lookup failed: " + e.toString());
			e.printStackTrace();
			System.exit(1);
		}
		try
		{
			topicConnection = topicConnectionFactory.createTopicConnection();
			topicSession = topicConnection.createTopicSession(false,
					Session.AUTO_ACKNOWLEDGE);
			topicPublisher = topicSession.createPublisher(topic);
			message = topicSession.createTextMessage();
			for (int i = 0; i < NUM_MSGS; i++)
			{
				message.setText("This is message " + (i + 1));
				System.out.println("Publishing message: " + message.getText());
				topicPublisher.publish(message);
			}
		}
		catch (JMSException e)
		{
			System.out.println("Exception occurred: " + e.toString());
		}
		finally
		{
			if (topicConnection != null)
			{
				try
				{
					topicConnection.close();
				}
				catch (JMSException e)
				{
				}
			}
		}
	}
}

*/